from flask import Flask,request
import requests
import json

from app.database.connections import hcapi,db
from app.models.models import Cpu
from app.models.model_memory import Memory
from app.models.model_disk import Disk
from app.models.model_network import Network
from app.models.model_machine import Machine

# harrier center api
# hcapi = Flask(__name__)

@hcapi.route('/hcapi', methods=['POST','GET'])
def api():
    global maccode
    global hardwarecode
    global data
    if request.method == "POST":
        maccode = request.form['maccode']
        hardwarecode = request.form['hardwarecode']
        data = request.form['data']
    else:
        maccode = request.args['maccode']
        hardwarecode = request.args['hardwarecode']
        data = request.args['data']

    data_json = json.loads(data)
    print("#####:" + str(data_json))

    j = "ok"
    if "cpu" == hardwarecode.lower():
        add_cpu(data_json)
    elif "memory" == hardwarecode.lower():
        add_memory(data_json)
    elif "disk" == hardwarecode.lower():
        add_disk(data_json)
    elif "network" == hardwarecode.lower():
        add_network(data_json)
    elif "machine" == hardwarecode.lower():
        add_machine(data_json)
    return j

def add_machine(machine_json):
    machine = Machine(machine_json['mac_code'], machine_json['machine_name'],'','',1)
    maccode = machine_json['mac_code']
    data = db.session.query(Disk).filter(Machine.mac_code == maccode).first()
    if not data :
        db.session.add(machine)
        db.session.commit()
    else:
        pass

def add_cpu(cpu_json):
    cpu = Cpu(cpu_json['mac_code'], 'Intel(R)Core(TM)i7-10510U CPU@1.80GHz', cpu_json['utilization'], cpu_json['speed'],
              cpu_json['processes'], cpu_json['threads'], cpu_json['handles'], cpu_json['start_machine_time_str'],
              cpu_json['base_speed'], 1, cpu_json['kernels'], cpu_json['logical_processors'])
    db.session.add(cpu)
    db.session.commit()

def add_memory(memory_json):
    memory = Memory(memory_json['mac_code'],memory_json['total_memory_size'],memory_json['available_size'],
                    memory_json['used_percent'],memory_json['used_size'],memory_json['memory_usage'],
                    memory_json['spare_size'],memory_json['swap_memory_total'],memory_json['swap_used_percent'],
                    memory_json['swap_used_size'],memory_json['swap_memory_usage'],memory_json['swap_spare_size'],
                    memory_json['swap_sin'],memory_json['swap_sout'])
    db.session.add(memory)
    db.session.commit()

def add_disk(disk_json):
    disk = Disk(disk_json['mac_code'],"INTEL SSDPEKNW010T8",disk_json['disk_parts'],disk_json['disk_total_size'],
                disk_json['disk_used'],disk_json['disk_free'],disk_json['disk_use_percent'],
                disk_json['io_read_count'],disk_json['io_write_count'],disk_json['io_read_size'],
                disk_json['io_write_size'],disk_json['io_read_time'],disk_json['io_write_time'])
    db.session.add(disk)
    db.session.commit()
    # print(disk_json)

def add_network(datalist):
    # r = requests.post(api_address + "network")
    # print(r.text)
    # datalist = json.loads(r.text)
    for network_json in datalist:
        print(network_json)
        # network_json = json.loads(n)
        network = Network(network_json['mac_code'],network_json['network_name'],network_json['bytes_sent'],
                        network_json['bytes_recv'],network_json['packets_sent'],network_json['packets_recv'],
                        network_json['errin'],network_json['errout'],network_json['dropin'],network_json['dropout'])
        db.session.add(network)
        db.session.commit()
    # print(network_json)

if __name__ == '__main__':
    hcapi.run(debug=True,host='0.0.0.0', port=6000)
    # hcapi.run(port=6000)